﻿using System;
using System.Collections.Generic;
using System.Net.Mime;
using System.Text;
using System.Threading;
using System.IO;

namespace Cassini
{
    class Program
    {
        static Server _server;
        static string _appPath;
        static int _portNumber;
        static string _virtRoot;

        static void Main(string[] args)
        {
            args = new string[] { "/output"};
            ParseArgs(args);
            var mutex = new Mutex(false, "mycassini" + _portNumber);
            var mutex2 = new Mutex(false, "mycassinirunning" + _portNumber);

            if (_appPath == "stop")
            {
                mutex.WaitOne();
                mutex2.WaitOne();

                mutex2.ReleaseMutex();
                mutex.ReleaseMutex();
                return;
            }

            try
            {
                _server = new Cassini.Server(_portNumber, _virtRoot, _appPath);
                mutex2.WaitOne();
                _server.Start();
                System.Diagnostics.Process.Start("http://localhost:"+_portNumber+ "/Index.aspx");
            }
            catch (Exception er)
            {
                Console.WriteLine(er);
            }

            while (mutex.WaitOne(0))
            {
                mutex.ReleaseMutex();
                Thread.Sleep(50);
            }

            mutex2.ReleaseMutex();
        }

        private static void ParseArgs(String[] args)
        {
            string portString = null;
            try
            {
                if (args.Length >= 1)
                {
                    _appPath = args[0].Trim();
                    if (_appPath[0] == '/')
                    {
                        _appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _appPath.Substring(1));
                    }
                }

                if (args.Length >= 2)
                    portString = args[1];

                if (args.Length >= 3)
                    _virtRoot = args[2];
            }
            catch
            {
            }

            if (portString == null)
                portString = "89";

            _portNumber = Int32.Parse(portString);
            if (_virtRoot == null)
                _virtRoot = "/";

            if (_appPath == null)
                _appPath = _appPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"WebFormApp");
        }

    }
}
